<template>
  <div class="app-container">
    <Find
      :OriginalFilter="{ static: true }"
      :formList="formList"
      @findClicked="findClicked"
      @Launchform="Launchform5"
      @clickNumber="clickNumber1"
      @clickNumber1="clickNumber"
    />

    <Find :searchImg="false" :formList="formList2" @findClicked="findClicked2" @Launchform="Launchform6" />

    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <h1>入库明细查询</h1>

      <Table
        @inputChange="checkboxChange2"
        :summaryData="summaryData"
        summary
        :isLoading="isLoading"
        selection
        :tableHeader="tableHeader"
        :tableData="inWareHouseMethodsTableDate"
        @selectList="selectList"
      />
      <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'RdHarvestCheck'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ElMessage } from 'element-plus'
import { VendorQuery } from '@/api/WarehouseDepartment/FactoryStorage/FitOutsourcingProcessing'
import { onMounted } from 'vue'
import { ref } from 'vue'
import {
  inWareHouse,
  checkAndCheck,
  FillingUnitPrice,
  FillingUnitPrice1,
  TypeList
} from '@/api/GeneralFinanceDepartment/RongyangFinancialOperations/RdHarvestCheck'

// 类型
const TypeListMethods = async () => {
  try {
    const result = await TypeList()
    formList.value[7].selectOptions = EvaluatesTheArrayReturnValue(result)
  } catch (error) {
    JMessage.error('类型查询出错')
  }
}

// 备注修改
const checkboxChange2 = async (list: any) => {
  if (list.sendQty !== null && list.price !== null) {
    list.amt = (list.sendQty * list.price).toFixed(4)
  }
}

const summaryData = ref<any[]>([
  {
    index: 17,
    num: 0
  },
  {
    index: 19,
    num: 0
  }
])

const FillingUnitPriceParms = ref<any>({
  list: [],
  discount: '',
  orgPrice: '',
  choose: ''
})

const FillingUnitPriceParms1 = ref<any>({
  list: [],
  discount: '',
  orgPrice: '',
  choose: '',
  sendDate: ''
})

const FillingUnitPriceMethods1 = async () => {
  FillingUnitPriceParms1.value.list = inWareHouseMethodsTableDate.value
  try {
    const result = await FillingUnitPrice1(
      FillingUnitPriceParms1.value.list,
      FillingUnitPriceParms1.value.discount,
      FillingUnitPriceParms1.value.orgPrice,
      FillingUnitPriceParms1.value.sendDate,
      FillingUnitPriceParms1.value.choose
    )
    if (result.code === 200) {
      inWareHouseMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('程序出错请重试')
  }
}

const findClicked2 = (row: any) => {
  if (row === '打勾记录检核') {
    checkAndCheckParms.value = 1
    if (checkAndCheckList.value.length === 0) {
      JMessage.warning('请勾选需要检核的数据')
    } else {
      checkAndCheckMethods()
    }
  }

  if (row === '打勾记录取消检核') {
    checkAndCheckParms.value = 2
    if (checkAndCheckList.value.length === 0) {
      JMessage.warning('请勾选需要取消检核的数据')
    } else {
      checkAndCheckMethods()
    }
  }

  if (row === '填充单价') {
    console.log(FillingUnitPriceParms.value.list)
    FillingUnitPriceParms.value.choose = 1
    if (FillingUnitPriceParms.value.discount === '' || FillingUnitPriceParms.value.orgPrice === '') {
      JMessage.warning('折扣比例或原单价不可为空')
    } else if (FillingUnitPriceParms.value.list.length === 0) {
      JMessage.warning('请勾选需要填充单价的数据')
    } else {
      FillingUnitPriceMethods()
    }
  }

  if (row === '填充送货日期和数量') {
    FillingUnitPriceParms1.value.choose = 2
    if (FillingUnitPriceParms1.value.sendDate === '') {
      JMessage.warning('送货日期不可为空')
    } else if (FillingUnitPriceParms1.value.list.length === 0) {
      JMessage.warning('请勾选需要填充单价的数据')
    } else {
      FillingUnitPriceMethods1()
    }
  }

  if (row === '保存') {
    FillingUnitPriceParms1.value.choose = 3
    FillingUnitPriceMethods1()
  }
}

const FillingUnitPriceMethods = async () => {
  try {
    const result = await FillingUnitPrice(
      FillingUnitPriceParms.value.list,
      FillingUnitPriceParms.value.discount,
      FillingUnitPriceParms.value.orgPrice,
      FillingUnitPriceParms.value.sendDate,
      FillingUnitPriceParms.value.choose
    )
    if (result.code === 200) {
      inWareHouseMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('程序出错请重试')
  }
}

const Launchform6 = (row: any) => {
  FillingUnitPriceParms.value.discount = Number(row[2].inputValue) === 0 ? '' : Number(row[2].inputValue)
  FillingUnitPriceParms.value.orgPrice = Number(row[3].inputValue) === 0 ? '' : Number(row[3].inputValue)
  FillingUnitPriceParms1.value.sendDate = row[5].dateValue
}

// 检核 取消检核
const checkAndCheckParms = ref<number>(0)

const selectList = (row: any) => {
  checkAndCheckList.value = row.map((item: any) => {
    return item
  })

  FillingUnitPriceParms.value.list = row.map((item: any) => {
    return item
  })

  FillingUnitPriceParms1.value.list = row.map((item: any) => {
    return item
  })
}

const checkAndCheckList = ref<any>([])

const checkAndCheckMethods = async () => {
  try {
    const result = await checkAndCheck(checkAndCheckList.value, checkAndCheckParms.value)
    if (result.code === 200) {
      inWareHouseMethods()
      checkAndCheckList.vaue = []
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('检核出错请重新检核')
  }
}

const findClicked = (a: string) => {
  if (a === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.dateValue = ''
      item.selectValue = ''
    })
  }
}

const clickNumber1 = (row: any) => {
  switch (row) {
    case '1': {
      inWareHouseParms.value.check = 2
      break
    }
    case '2': {
      inWareHouseParms.value.check = 1
      break
    }
    case '0': {
      inWareHouseParms.value.check = 0
      break
    }
  }
  inWareHouseMethods()
}

const clickNumber = (row: any) => {
  switch (row) {
    case '0': {
      inWareHouseParms.value.sendC = 2
      break
    }
    case '1': {
      inWareHouseParms.value.sendC = 1
      break
    }
    case '2': {
      inWareHouseParms.value.sendC = 9
      break
    }
  }
  inWareHouseMethods()
}

// 厂商查询
const VendorQueryMethods = async () => {
  let NewArray = []
  const result = await VendorQuery()
  NewArray = result.reduce((arr: any, item: any) => {
    if (item.vendorNo && item.vendorName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.vendorName)
    }
    return arr
  }, [])
  formList.value[2].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
}

const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

const Launchform5 = (row: any) => {
  inWareHouseParms.value.purNo = row[0].inputValue
  inWareHouseParms.value.purStartDate = row[1].dateValue[0]
  inWareHouseParms.value.purEndDate = row[1].dateValue[1]
  inWareHouseParms.value.vendorName = row[2].selectValue
  inWareHouseParms.value.lotNo = row[3].inputValue
  inWareHouseParms.value.mtlName = row[4].inputValue
  inWareHouseParms.value.sendNo = row[5].inputValue
  inWareHouseParms.value.sendM = row[6].inputValue
  inWareHouseParms.value.type = row[7].selectValue
  inWareHouseMethods()
}

const isLoading = ref(false)

// 入库明细查询
const inWareHouseParms = ref({
  check: 2,
  lotNo: '',
  mtlName: '',
  pageNum: 1,
  pageSize: 40,
  purEndDate: '',
  purNo: '',
  purStartDate: '',
  sendC: 2,
  sendM: '',
  sendNo: '',
  type: '',
  vendorName: ''
})

const inWareHouseMethodsTableDate = ref([])

const inWareHouseMethods = async () => {
  isLoading.value = true
  try {
    const result = await inWareHouse(inWareHouseParms.value)
    summaryData.value[0].num = result.sendQtyTotal
    summaryData.value[1].num = result.amtTotal

    inWareHouseMethodsTableDate.value = result.list.map((item: any) => {
      item.change = 'Y'
      item.disableConfirm = true
      return item
    })
    total.value = result.total
    isLoading.value = false
  } catch (error) {
    JMessage.error('入库明细查询出错!')
    isLoading.value = false
  }
}

const total = ref(0)
const handleSizeChange = (val: number) => {
  inWareHouseParms.value.pageSize = val
  inWareHouseMethods()
}
const handleCurrentChange = (val: number) => {
  inWareHouseParms.value.pageNum = val
  inWareHouseMethods()
}

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '订购单号',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '订购日期',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '送货单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '送货月份',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },

  {
    type: 'radio',
    checkboxLabel: '未检核',
    checked: false,
    label: '1'
  },
  {
    type: 'radio',
    checkboxLabel: '已检核',
    checked: false,
    label: '2'
  },
  {
    type: 'radio',
    checkboxLabel: '全部',
    checked: false,
    label: '0'
  },

  {
    type: 'radio1',
    checkboxLabel: '未收货',
    checked: false,
    label: '0'
  },
  {
    type: 'radio1',
    checkboxLabel: '已收货',
    checked: false,
    label: '1'
  },
  {
    type: 'radio1',
    checkboxLabel: '全部',
    checked: false,
    label: '2'
  }
])

const formList2 = ref([
  {
    type: 'button',
    checkboxLabel: '打勾记录检核'
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录取消检核'
  },
  {
    type: 'input',
    inputPlaceholder: '折扣比例',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '原单价',
    inputValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '填充单价'
  },
  {
    type: 'dateSingle',
    dateLabel: '送货日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '填充送货日期和数量'
  },
  {
    type: 'button',
    checkboxLabel: '保存'
  }
])

const tableHeader = ref([
  {
    change: true,
    label: '检核标志',
    prop: 'cfmFlag'
  },
  {
    change: true,
    label: '类型',
    prop: 'purType'
  },
  {
    change: true,
    label: '订购单号',
    prop: 'purNo'
  },
  {
    change: true,
    label: '订购日期',
    prop: 'purDate'
  },
  {
    change: true,
    label: '厂商名称',
    prop: 'vendorName'
  },
  {
    change: true,
    label: '指令',
    prop: 'orderId'
  },
  {
    change: true,
    label: '型体',
    prop: 'style'
  },

  {
    change: true,
    label: '品牌',
    prop: 'brand'
  },

  {
    change: true,
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    change: true,
    width: 250,
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    change: true,
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    change: true,
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    change: true,
    label: '用量',
    prop: 'usage'
  },
  {
    change: true,
    label: '请购数量',
    prop: 'purQty'
  },
  {
    label: '送货日期',
    prop: 'sendDate'
  },
  {
    label: '送货单号',
    prop: 'sendNo'
  },
  {
    label: '收货数量',
    prop: 'sendQty'
  },
  {
    label: '折扣单价',
    prop: 'price'
  },
  {
    change: true,
    label: '金额',
    prop: 'amt'
  },
  {
    label: '原单价',
    prop: 'orgPrice'
  },
  {
    label: '财务备注',
    prop: 'financeRecMemo'
  },
  {
    change: true,
    label: '采购备注',
    prop: 'remark'
  },
  {
    change: true,
    label: '采购员',
    prop: 'sysUser'
  },

  {
    change: true,
    label: '指令数',
    prop: 'orderTotalQty'
  },

  {
    change: true,
    label: '责任人',
    prop: 'dutyUser'
  },

  {
    change: true,
    label: '责任原因',
    prop: 'dutyRek'
  }
])

onMounted(() => {
  // inWareHouseMethods()

  VendorQueryMethods()

  TypeListMethods()
})
</script>

<style lang="less" scoped>
h1 {
  font-size: 16px;
}
</style>
